/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ems.web;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.constant.Constant;
import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.ems.entity.EmsStudent;
import com.thinkgem.jeesite.modules.ems.service.EmsStudentService;

/**
 * 学员Controller
 * @author zad
 * @version 2018-01-25
 */
@Controller
@RequestMapping(value = "${adminPath}/ems/emsStudent")
public class EmsStudentController extends BaseController {

	@Autowired
	private EmsStudentService emsStudentService;
	
	@ModelAttribute
	public EmsStudent get(@RequestParam(required=false) String id) {
		EmsStudent entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = emsStudentService.get(id);
		}
		if (entity == null){
			entity = new EmsStudent();
		}
		return entity;
	}
	
	@RequiresPermissions("ems:emsStudent:view")
	@RequestMapping(value = {"list", ""})
	public String list(EmsStudent emsStudent, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EmsStudent> page = emsStudentService.findPage(new Page<EmsStudent>(request, response), emsStudent); 
		model.addAttribute("page", page);
		return "modules/ems/emsStudentList";
	}

	@RequiresPermissions("ems:emsStudent:view")
	@RequestMapping(value = "form")
	public String form(EmsStudent emsStudent, Model model) {
		
		
		// add by zad 2018-01-30 添加时初始化学号等信息
		if(null == emsStudent || StringUtils.isBlank(emsStudent.getId()))
		{
			if(null == emsStudent)
			{
				emsStudent = new EmsStudent();
			}
			// 添加时，当学号为空才生成，否则不再生成
			if(StringUtils.isBlank(emsStudent.getNo())){
				emsStudent.setNo(this.emsStudentService.generateStuNo());
			}
		}
		
		model.addAttribute("emsStudent", emsStudent);
		return "modules/ems/emsStudentForm";
	}

	@RequiresPermissions("ems:emsStudent:edit")
	@RequestMapping(value = "save")
	public String save(EmsStudent emsStudent, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, emsStudent)){
			return form(emsStudent, model);
		}
		
		
		// add by zad 2018-01-30 添加学员时，新添加的学员为新生
		emsStudent.setIsNew(Constant.Dict.DICT_BOOLEAN_YES);
		
		
		emsStudentService.save(emsStudent);
		addMessage(redirectAttributes, "保存学员成功");
		return "redirect:"+Global.getAdminPath()+"/ems/emsStudent/?repage";
	}
	
	@RequiresPermissions("ems:emsStudent:edit")
	@RequestMapping(value = "delete")
	public String delete(EmsStudent emsStudent, RedirectAttributes redirectAttributes) {
		emsStudentService.delete(emsStudent);
		addMessage(redirectAttributes, "删除学员成功");
		return "redirect:"+Global.getAdminPath()+"/ems/emsStudent/?repage";
	}
	
	
	@RequiresPermissions("ems:emsStudent:view")
	@RequestMapping(value = {"checkExists"})
	public String checkExists(EmsStudent emsStudent, HttpServletRequest request, HttpServletResponse response, Model model) {
		
		Map<String,String> rs = new HashMap<String,String>();
		List<EmsStudent> students = this.emsStudentService.checkExists(emsStudent);
		if(null != students && students.size() > 0)
		{
			rs.put("rs", "yes");
		}
		else
		{
			rs.put("rs", "no");
		}
		
		String jsonStr = JsonMapper.toJsonString(rs);
		try {
			response.getWriter().print(jsonStr);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}